import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/login_module/logic.dart';
import 'package:exams_app/widget/hexcolor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'dart:convert';
import 'package:exams_app/home_module/account_set_module/updata_account_page.dart';
import 'package:exams_app/home_module/account_set_module/buliding_mobule_page.dart';
import 'about_page.dart';
import 'fack_back_page.dart';
import 'account_logic.dart';

// class AccountSettingPage extends StatefulWidget {
//   @override
//   State<StatefulWidget> createState() {
//     // TODO: implement createState
//     return AccountSettingStatePage();
//   }
// }

class AccountSettingPage extends StatelessWidget {


/// 依赖注入
  final AccountLogic logic = Get.put(AccountLogic());

  // @override
  // void initState() {
  //   // TODO: implement initState
  //   super.initState();
  //
  //   logic.requestDataWithUserInfo();
  //   logic.requestDataWithUser();
  // }


  /// 页面构建
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[200],
        appBar: AppBar(
          title: Text('账号设置'),
          centerTitle: true,
        ),
        body: GetBuilder<AccountLogic>(
          initState: (sz){
            logic.requestDataWithUser();
            logic.requestDataWithUserInfo();
          },
          builder: (logic) {
            return ListView(
              children: [
                GestureDetector(
                  onTap: () {
                    print('1111');
                    /// 弹出框
                    showModalBottomSheet(
                        backgroundColor: Colors.transparent,
                        isScrollControlled: true,
                        context: context,
                        builder: (BuildContext context) {
                          return StatefulBuilder(builder: (context1, state) {
                            return Container(
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(10.0)),
                              ),
                              child: Column(
                                children: [
                                  TextButton(
                                    child: Text(
                                      '拍照',
                                      style: TextStyle(
                                          color:
                                              Theme.of(context).primaryColor),
                                    ),
                                    onPressed: () async {
                                      logic.openCamera().then((value)async {
                                        List bytes = await value.readAsBytes();

                                        String bs64 = base64Encode(bytes);

                                        String bs64Image = bs64;

                                        // logic.UpdataDataWithHeader(bs64Image);
                                        logic.requestDataWithapi_my_write('image_1920', bs64Image);


                                        logic.imageHeader.value = value.path;

                                      });
                                      Get.back();
                                    },
                                  ),
                                  Divider(
                                    color: Colors.grey[200],
                                  ),
                                  TextButton(
                                    child: Text('从相册选择',
                                        style: TextStyle(
                                            color: Theme.of(context)
                                                .primaryColor)),
                                    onPressed: () async {
                                      logic.openGallery().then((value) async {
                                        List bytes = await value.readAsBytes();

                                        String bs64 = base64Encode(bytes);

                                        String bs64Image = bs64;

                                        logic.requestDataWithapi_my_write('image_1920', bs64Image);

                                        logic.imageHeader.value = value.path;

                                      });

                                      Get.back();
                                    },
                                  ),
                                  Divider(
                                    color: Colors.grey[200],
                                    thickness: 10,
                                    height: 30,
                                  ),
                                  TextButton(
                                    child: Text(
                                      '取消',
                                      style:
                                          TextStyle(color: HexColor('#515151')),
                                    ),
                                    onPressed: () => Get.back(),
                                  ),
                                ],
                              ),
                              height: 200,
                            );
                          });
                        });
                  },
                  child: Container(
                    padding: EdgeInsets.only(top: 15, left: 10, right: 15),
                    color: Colors.white,
                    height: 100,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                           logic.imageHeader.value==''?ClipOval(
                              child: Image.network(
                                logic.userJson['data'][0]
                                ['image_128'],
                                fit: BoxFit.cover,
                                width: 50,
                                height: 50,
                              ),
                            ):ClipOval(
                             child: Image.file(File(logic.imageHeader.value),width: 50,height: 50,fit: BoxFit.cover,),
                           ),
                            SizedBox(
                              width: 5,
                            ),
                            Text('${logic.rzmodel.value.data[0].name}'),
                          ],
                        ),
                        Icon(
                          Icons.arrow_forward_ios_sharp,
                          size: 15,
                        ),
                      ],
                    ),
                  ),
                ),

                Container(
                  margin: EdgeInsets.only(
                    top: 5,
                  ),
                  child: ListTile(
                      title: Text('真实姓名'),
                      trailing: Container(
                        width: 111,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Text(
                              '${logic.rzmodel.value.data[0].fullName}',
                              style: TextStyle(
                                  fontSize: 16, color: HexColor('#999999')),
                            ),
                            // Icon(
                            //   Icons.arrow_forward_ios_sharp,
                            //   size: 15,
                            // ),
                          ],
                        ),
                      )),
                  color: Colors.white,
                ),
                Container(
                  margin: EdgeInsets.only(
                    top: 5,
                  ),
                  child: ListTile(
                      onTap: () async {
                        var data = await Get.to(UpdataAccountPage(nickname: logic.rzmodel.value.data[0].name,));
                        if (data == 'refresh') {
                          logic.requestDataWithUser();
                          logic.requestDataWithUserInfo();

                          // logic.requestDataWithUserInfo().then((value) {
                          //   logic.json = value;
                          //   // setState(() {
                          //
                          //   // });
                          // });
                        }
                      },
                      title: Text(
                        '昵称',
                      ),
                      trailing: Container(
                        alignment: Alignment.centerLeft,
                        width: 200,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Text(
                              '${logic.rzmodel.value.data[0].name}',
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: TextStyle(
                                  fontSize: 16, color: HexColor('#999999')),
                            ),
                            Icon(
                              Icons.arrow_forward_ios_sharp,
                              size: 15,
                            ),
                          ],
                        ),
                      )),
                  color: Colors.white,
                ),
                Container(
                  margin: EdgeInsets.only(
                    top: 5,
                  ),
                  child: ListTile(
                      onTap: () async {
                        var data = await Get.to(BulidingMoblePage());
                        if (data == 'refresh') {

                          logic.requestDataWithUserInfo();
                          logic.requestDataWithUser();

                          // logic.requestDataWithUserInfo().then((value) {
                          //   logic.json = value;
                          //   // setState(() {
                          //   //
                          //   // });
                          // });
                        }
                      },
                      title: Text('手机号码'),
                      trailing: Container(
                        width: 150,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Text(
                              '${logic.userJson['data'][0]['login_mobile']}',
                              style: TextStyle(
                                  fontSize: 16, color: HexColor('#999999')),
                            ),
                            Icon(
                              Icons.arrow_forward_ios_sharp,
                              size: 15,
                            ),
                          ],
                        ),
                      )),
                  color: Colors.white,
                ),
                Container(
                  margin: EdgeInsets.only(
                    top: 5,
                  ),
                  child: ListTile(
                    onTap: () async {
                      var data = await Get.to(UpdataAccountPasswordPage());
                    },
                    title: Text('密码修改'),
                    trailing: Icon(
                      Icons.arrow_forward_ios_sharp,
                      size: 15,
                    ),
                  ),
                  color: Colors.white,
                ),
                Container(
                  margin: EdgeInsets.only(
                    top: 5,
                  ),
                  child: ListTile(
                    onTap: () {
                      Get.to(FackBackPage());
                    },
                    title: Text('意见反馈'),
                    trailing: Icon(
                      Icons.arrow_forward_ios_sharp,
                      size: 15,
                    ),
                  ),
                  color: Colors.white,
                ),
                Container(
                  margin: EdgeInsets.only(
                    top: 5,
                  ),
                  child: ListTile(
                    onTap: () {
                      Get.to(AboutPage());
                    },
                    title: Text('关于'),
                    trailing: Icon(
                      Icons.arrow_forward_ios_sharp,
                      size: 15,
                    ),
                  ),
                  color: Colors.white,
                ),
              ],
            );
          },
        ));
  }
}
